import { login } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import Crypto from '@/utils/crypto.js'

const user = {
  state: {
    token: getToken(),
    name: '123123',
    avatar: '',
    roles: [],
    permissions: [],
    deptName: '',
    areaId: '',
    isFirstLogin: 'NO',
    userId: '',
    userName: '',
    idCard: ''
  },

  mutations: {
    SET_ISFIRSTLOGIN: (state, isFirstLogin) => {
      state.isFirstLogin = isFirstLogin
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_USERNAME: (state, userName) => {
      state.userName = userName
    },
    SET_IDCARD: (state, idCard) => {
      state.idCard = idCard
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName
    },
    SET_AREAID: (state, areaId) => {
      state.areaId = areaId
    },
    SET_DEPTCOMPANYRELS: (state, deptCompanyRels) => {
      state.deptCompanyRels = deptCompanyRels
    }
  },

  actions: {
    // 测试的登录
    testLogin ({ commit }) { // 无后台时使用
      localStorage.setItem('token', 'res.data.token')
      localStorage.setItem('userId', 'res.data.userId')
      localStorage.setItem('userName', '李世民')
      localStorage.setItem('idCard', '445381199808097511')
      localStorage.setItem('bankAccount', '987654321')
      setToken('res.data.token')
      commit('SET_TOKEN', 'res.data.token')
      commit('SET_USERNAME', '李世民')
      commit('SET_IDCARD', '445381199808097511')
      console.log('获取身份证号')
      console.log(localStorage.getItem('idCard'))
    },
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          if (res.code !== 200) { // 密码不正确直接返回
            resolve(res)
            return
          }
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('userId', res.data.userId)
          localStorage.setItem('userName', res.data.userName)
          localStorage.setItem('idCard', Crypto.decrypt64(res.data.idCard, 'tecsun#2022abcde', 'edcba2202#nuscet'))
          localStorage.setItem('bankAccount', Crypto.decrypt64(res.data.bankAccount, 'tecsun#2022abcde', 'edcba2202#nuscet'))
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          commit('SET_USERNAME', res.data.userName)
          commit('SET_IDCARD', Crypto.decrypt64(res.data.idCard, 'tecsun#2022abcde', 'edcba2202#nuscet'))
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    LogOut ({ commit }) { // 清空登录数据
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_USERNAME', '')
        commit('SET_IDCARD', '')
        removeToken()
        localStorage.removeItem('token')
        localStorage.removeItem('userId')
        localStorage.removeItem('userName')
        localStorage.removeItem('idCard')
        localStorage.removeItem('bankAccount')
        resolve('退出登录成功！')
      })
    }
  }
}

export default user
